package com.seocoo.worker.base.city;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.sfh.lib.ui.MVVMActivity;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

/***
 * 选择城市列表
 */
public class SelectCityActivity extends MVVMActivity implements AMapLocationListener, OnPickListener {

    public static void start(Activity context, int requestCode) {
        Intent intent = new Intent(context, SelectCityActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    public static void start(Fragment context, int requestCode) {
        Intent intent = new Intent(context.getContext(), SelectCityActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    //声明mlocationClient对象
    public AMapLocationClient mlocationClient;

    CityPicker cityPicker;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AMapLocationClientOption mLocationOption = null;
        mlocationClient = new AMapLocationClient(this);
        mLocationOption = new AMapLocationClientOption();
        mlocationClient.setLocationListener(this);
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mlocationClient.setLocationOption(mLocationOption);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setOnceLocationLatest(true);
        mlocationClient.startLocation();

        cityPicker = CityPicker.from(this).setOnPickListener(this);
        cityPicker.show();
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                amapLocation.getLatitude();//获取纬度
                amapLocation.getLongitude();//获取经度
                amapLocation.getAccuracy();//获取精度信息
                cityPicker.locateComplete(new LocatedCity(amapLocation.getCity(), amapLocation.getProvince(), amapLocation.getCityCode()),LocateState.SUCCESS);//APP自身已定位的城市，传null会自动定位（默认）
            }
        }
    }

    @Override
    public void onPick(int position, City data) {
        Intent intent = new Intent();
        intent.putExtra("code", data.getCode());
        intent.putExtra("name", data.getName());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onLocate() {
        mlocationClient.startLocation();
    }

    @Override
    public void onCancel() {

        finish();
    }
}
